//
//  RHMePlantInfoDetailCell.m
//  RuiHeApp
//
//  Created by Yan Chen on 2025/1/9.
//

#import "RHMePlantInfoDetailCell.h"
#import "RHMePlantInfoModel.h"
#import "RHMeOptionTypeModel.h"

@interface RHMePlantInfoDetailCell()<QMUITextFieldDelegate>


@property (nonatomic ,strong) QMUILabel *titleLab;

@property (nonatomic ,strong) QMUITextField *textField;

@property (nonatomic ,strong) UIImageView *arrowImg;

@property (nonatomic ,strong) QMUIButton *clickBtn;


@end
@implementation RHMePlantInfoDetailCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setupUI];
    }
    return self;
}

-(void)setCornerBorderWithIsFirstCell:(BOOL)state
{
    if (state) {
        self.bgView.layer.cornerRadius = 8;
        self.bgView.qmui_borderColor = kBorderColor;
        self.bgView.qmui_borderWidth = 1;
        self.bgView.qmui_borderPosition = QMUIViewBorderPositionLeft|QMUIViewBorderPositionTop|QMUIViewBorderPositionRight;
//        self.bgView.layer.maskedCorners = kCALayerMinXMinYCorner|kCALayerMaxXMinYCorner;
        self.bgView.layer.qmui_maskedCorners = QMUILayerMinXMinYCorner|QMUILayerMaxXMinYCorner;
        self.bgView.layer.masksToBounds = YES;
    }else{
        self.bgView.layer.cornerRadius = 0;
        self.bgView.qmui_borderColor = kBorderColor;
        self.bgView.qmui_borderWidth = 1;
        self.bgView.qmui_borderPosition = QMUIViewBorderPositionLeft|QMUIViewBorderPositionRight;
        
        self.bgView.layer.masksToBounds = YES;
    }
}

-(void)setModel:(RHMePlantInfoModel *)model
{
    _model = model;
    self.titleLab.text = model.title;
    self.textField.placeholder = model.placeHolder;
    
    __block NSString *content;
    
    content = ValidNullDataToString(model.content, @"--");
    if (model.unit) {
        content = [NSString stringWithFormat:@"%@ %@",ValidNullDataToString(model.content, @"--"),model.unit];
    }
    if (model.isSelection) {
        [model.optionList enumerateObjectsUsingBlock:^(RHMeOptionTypeModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([model.content integerValue] == obj.typeId) {
                content = obj.typeName.rh_localized;
            }
        }];
    }
    [_textField mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);
    }];
    self.arrowImg.hidden = YES;
    self.clickBtn.hidden = NO;
    self.clickBtn.enabled = model.isEdit;
    self.textField.enabled = model.isEdit;
    if (model.isEdit) {
        if (model.canEdit) {
            self.textField.textColor = kCOLOR_HEX(@"#303133");
            if (model.isSelection) {
                self.arrowImg.hidden = NO;
                [_textField mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.right.mas_offset(-35);
                }];
                if ([model.tag isEqualToString:@"plant_city"]) {
                    self.clickBtn.hidden = NO;
                    content = ValidNullDataToString(model.content, nil);
                }
            }else{
                //编辑状态下,非选择项
                self.clickBtn.hidden = YES;
                content = ValidNullDataToString(model.content, nil);
            }
            
        }else{
            self.textField.textColor = kCOLOR_HEX(@"#8B8D92");
        }
        
    }else{
        self.textField.textColor = kCOLOR_HEX(@"#303133");
    }
    self.textField.text = content;
}


- (void)setupUI{
    kWeakSelf;
    self.contentView.backgroundColor = CFTabViewBgColor;
    UIView *bgView = [[UIView alloc] init];
    [bgView setBackgroundColor:UIColor.whiteColor];
    [self.contentView addSubview:bgView];
    self.bgView = bgView;
    
    _titleLab = [[QMUILabel alloc] init];
    [_titleLab setTextAlignment:NSTextAlignmentLeft];
    _titleLab.numberOfLines = 0;
    [_titleLab setFont:[UIFont systemFontOfSize:14]];
    [_titleLab setTextColor:kCOLOR_HEX(@"3F4443")];
    [bgView addSubview:_titleLab];
    
    
     _arrowImg= [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrow"]];
    [bgView addSubview:_arrowImg];
    [_arrowImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(15);
        make.centerY.mas_offset(0);
        make.right.mas_offset(-15);
    }];
 
    _textField = [[QMUITextField alloc] init];
    [_textField setTextAlignment:NSTextAlignmentRight];
    [_textField setTextColor:[kCOLOR_HEX(@"3F4443") colorWithAlphaComponent:0.8]];
    [_textField setFont:[UIFont systemFontOfSize:14]];
    [_textField setPlaceholder:@"Please input"];
    _textField.delegate = self;
    [bgView addSubview:_textField];
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleLab.mas_right).offset(5);
        make.right.mas_offset(-35);
        make.height.mas_equalTo(30);
        make.centerY.mas_offset(0);
    }];
    
    
    UIView *lineV = [[UIView alloc] init];
    [lineV setBackgroundColor:CFCellLineColor];
    [bgView addSubview:lineV];
    
        
    _clickBtn = [[QMUIButton alloc]init];
    [bgView addSubview:_clickBtn];
    [_clickBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(10, 15, 10, 15));
    }];
    
    [[_clickBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if(weakSelf.clickBlock){
            weakSelf.clickBlock();
        }
    }];
    

    
   
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0,15, 0, 15));
        make.height.mas_equalTo(50);
    }];
    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.centerY.mas_offset(0);
        make.width.mas_equalTo(kScaleW(100));
    }];
    
    
    
    [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.bottom.mas_offset(0);
        make.height.mas_equalTo(1);
    }];
    

}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    self.model.content = textField.text;
    //电站经纬度判断 经度-180 到180  纬度-90 到 90
    if ([self.model.tag isEqualToString:@"plant_longitude"]) {
        if ([textField.text doubleValue]<-180) {
            textField.text = @"-180";
            self.model.content = textField.text;
        }
        if ([textField.text doubleValue]>180) {
            textField.text = @"180";
            self.model.content = textField.text;
        }
    }
    if ([self.model.tag isEqualToString:@"plant_latitude"]) {
        if ([textField.text doubleValue]<-90) {
            textField.text = @"-90";
            self.model.content = textField.text;
        }
        if ([textField.text doubleValue]>90) {
            textField.text = @"90";
            self.model.content = textField.text;
        }
    }
}

@end
